package cn.shop.front.controller;

import cn.hutool.core.util.StrUtil;
import cn.shop.front.service.AlipayService;
import cn.shop.front.service.FrontUserService;
import cn.shop.front.util.CurrentUserUtil;
import cn.shop.transcation.alipay.entity.AlipayAccount;
import cn.shop.transcation.frontUserModule.FrontUser;
import cn.shop.utils.ApiCode;
import cn.shop.utils.ApiResult;
import cn.shop.utils.annotation.OperationLog;
import cn.shop.utils.enums.OperationType;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 支付宝绑定接口
 *
 * @author liyihan
 */
@RestController
@RequestMapping("/api/v1/alipay")
public class AlipayController {

    @Resource
    private AlipayService alipayService;
    @Resource
    private FrontUserService frontUserService;
    private static final int MAX_BANK_COUNT = 5;

    /**
     * 绑定支付宝
     *
     * @param account 支付宝信息
     * @return 返回结果
     */
    @PostMapping("")
    @OperationLog(desc = "绑定支付宝", type = OperationType.CREATE)
    public ApiResult<Map<String, Object>> insert(@RequestBody AlipayAccount account) {
        long uid = CurrentUserUtil.getUserId();
        long count = alipayService.getAliAccountCount(uid);
        if (count >= MAX_BANK_COUNT) {
            return ApiResult.fail(ApiCode.FAIL, "支付宝账号最多只能添加5个");
        }
        FrontUser user = frontUserService.getById(uid);
        if (user == null || StrUtil.isBlank(user.getIdentityCard())) {
            return ApiResult.fail("请先进行实名认证", null);
        }
        if (StrUtil.isBlank(user.getPhone())) {
            return ApiResult.fail("请先绑定手机号", null);
        }
        if (StrUtil.isBlank(user.getName())) {
            return ApiResult.fail("请先完善基本信息");
        }
        /*if (!user.getName().equals(account.getName())) {
            return ApiResult.fail("只能绑定本人支付宝");
        }*/
        account.setOwnerId(uid);
        return ApiResult.result(alipayService.save(account));
    }

    /**
     * 查看已绑定支付宝
     *
     * @return 返回结果
     */
    @GetMapping("")
    public ApiResult<List<Map<String, Object>>> list() {
        long uid = CurrentUserUtil.getUserId();
        List<Map<String, Object>> list = alipayService.aliAccountList(uid);
        return ApiResult.success(list);
    }

    /**
     * 解绑支付宝
     *
     * @param id 编号
     * @return 返回结果
     */
    @DeleteMapping("/{id}")
    @OperationLog(desc = "删除支付宝", type = OperationType.DELETE)
    public ApiResult<Map<String, Object>> delete(@PathVariable long id) {
        AlipayAccount delete = new AlipayAccount();
        delete.setOwnerId(CurrentUserUtil.getUserId());
        delete.setId(id);
        return ApiResult.result(alipayService.removeById(delete));
    }


}
